/* Layout */
// import { routerMap } from "./routes";
import store from "store";

// 页面路由注册
import viewRouter from "@/views/index.js";
// 加载路由菜单,从localStorage拿到路由,在创建路由时使用
export const dynamicRouterAdd = () => {
	let data = store.get("menuGroup");
	if (!data) return [];
	return filterAsyncRouter(data)
		.reduce((prev, curr) => [...prev, ...curr.children], [])
		.filter(v => !!v.path);
};

const hiddenRoutes = [];

// @函数: 遍历后台传来的路由字符串，转换为组件对象
export const filterAsyncRouter = asyncRouterMap => {
	let accessedRouters = [];
	if (asyncRouterMap) {
		asyncRouterMap.forEach(item => {
			let accessedRouter = {
				meta: item.meta,
				children: []
			};
			if (hasChild(item)) {
				accessedRouter.children = filterAsyncRouter(item.children);
			}
			let route = viewRouter.find(v => v.path == item.path);
			accessedRouter = {
				...accessedRouter,
				...route
			};
			accessedRouters.push(accessedRouter);
		});
	}

	return accessedRouters;
};
export const hasChild = item => {
	return item.children && item.children.length !== 0;
};
